<%@ page language="java" import="java.util.*,org.dom4j.*" pageEncoding="GB2312"%>
<%@ page import="org.lab.xmlparser.Parserxml"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'parser.jsp' starting page</title>
    <script type="text/javascript" src="<%=path %>/js/jquery.js"></script>
	<script type="text/javascript" src="<%=path %>/js/dtree.js"></script>
	<link rel="stylesheet" type="text/css" href="<%=path %>/css/dtree.css">
	<link rel="stylesheet" type="text/css" href="<%=path %>/css/styles.css">
<%
String name2 = "";
String name1 = "";
if(request.getParameter("name1")!=null){
	name2 = request.getParameter("name1"); 
	name1 = new String(name2.getBytes("ISO-8859-1"),"GB2312");
	System.out.println("in parser the name1 is:" + name1);}%>
	
  </head>
  
  <body>
  <% if(!"".equals(name1)){%>
   <script type="text/javascript">
			d = new dTree('d');
		
		d.add(0,-1,'PD Model');
		<% Parserxml pxml = new Parserxml();
	
		Document doc = pxml.getDocument(getServletContext().getRealPath("/pdmodel/"+name1+".xml"));
		System.out.println("this address is:" + getServletContext().getRealPath("/pdmodel/"+name1+".xml"));
		   Element eleName = pxml.getNode(doc,"//xpdl/Package/WorkflowProcess/Name");
		   String name = eleName.getText();
		   %>
		  d.add(1,0,'<%=name%>','','','','','','');
			<%	Element ele = pxml.getNode(doc,"//xpdl/Package/WorkflowProcess");
				List<Element> list = ele.elements();
				List<String> activityName = new LinkedList<String>();
				List<String> transitionName = new LinkedList<String>();
				
				for(int i = 0 ; i < list.size();i++){
					Element elem = list.get(i);
					if("Activity".equals(elem.getName())){
						List<Element> l = elem.elements();
						for(int j = 0 ; j < l.size() ; j++){
							if("Name".equals(l.get(j).getName())&&l.get(j).getText().length()!=0){
								activityName.add(l.get(j).getText());
								
							}
							
							
						}
					}
					if("Transition".equals(elem.getName())){
						List<Element> li = elem.elements();
						for(int j = 0; j < li.size(); j++){
							if("Name".equals(li.get(j).getName())){
								transitionName.add(li.get(j).getText());
							}
						}
					}
				}
			%>
			<%int num = 0;
			
			for(int i = 0 ; i < activityName.size(); i++){%>
			
			d.add(<%=i+2%>,1,'<%=activityName.get(i)%>','','','','<%=path%>/image/treeviewimg/activity.gif','<%=path%>/image/treeviewimg/activity.gif','');
			
			<% num=num+1;}%>
				
			<%int num1 = num;
			for(int i = 0 ; i < transitionName.size(); i++){%>
			
			d.add(<%=i+2+num%>,1,'<%=transitionName.get(i)%>','','','','<%=path%>/image/treeviewimg/transition.gif','<%=path%>/image/treeviewimg/transition.gif','');
			
			<%  num1 = num1 + 1;}%>
			
			<%Element eleAct = pxml.getNode(doc,"//xpdl/Package/WorkflowProcess/Activity");
				List <Element> listAct = eleAct.elements();
				int num2 = 0;
				for(int i = 0; i < num; i++){
					for(int j = 0; j < listAct.size();j++){%>
			d.add(<%=j+2+num1%>,<%=i+2%>,'<%=listAct.get(j).getName()%>');	
					<%num2 = num2+1;}
					
				}
			%>
			<%Element eleTra = pxml.getNode(doc,"//xpdl/Package/WorkflowProcess/Transition");
				List <Element> listTra = eleTra.elements();
				int num3 = num1-num;
				for(int i = 0; i < num3; i++){
					for(int j = 0; j < listTra.size();j++){%>
			d.add(<%=j+2+num2%>,<%=i+2+num%>,'<%=listTra.get(j).getName()%>');	
					<%}
					
				}
			%>
		document.write(d);
	</script>
	<%} %>
	
  </body>
</html>
